package com.bci.pwtz.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;

import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.DyzsblqMapper;
import com.bci.pwtz.mysql.model.Dyzsblq;
import com.bci.pwtz.service.BusinessRefService;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.DyzsblqService;

public class DyzsblqServiceImpl implements DyzsblqService{
	private DyzsblqMapper objMapper;
	private BusinessRefService businessRefService;
	private CompanyService companyService;
	private static final Logger _logger=Logger.getLogger(DyzsblqServiceImpl.class);
	public DyzsblqMapper getObjMapper() {
		return this.objMapper;
	}

	public void setObjMapper(DyzsblqMapper objMapper) {
		this.objMapper = objMapper;
	}

	public BusinessRefService getBusinessRefService()
    {
        return businessRefService;
    }

    public void setBusinessRefService(BusinessRefService businessRefService)
    {
        this.businessRefService = businessRefService;
    }

    public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	@Override
	public Dyzsblq load(String dyzsblqId) throws PwtzDatabaseException {
		if(_logger.isDebugEnabled()){
			_logger.debug("load:dyzsblqId="+dyzsblqId);
		}
		Dyzsblq dyzsblq=null;
		try{
			dyzsblq=objMapper.selectByPrimaryKey(dyzsblqId);
		}catch(DataAccessException e){
			_logger.error(ExceptionUtils.getStackTrace(e));
			throw new PwtzDatabaseException();
		}
		if(dyzsblq!=null){
			_logger.debug("load:result dyzsblq="+dyzsblq.toString());
		}
		return dyzsblq;
	}

	@Override
	public Dyzsblq add(Dyzsblq dyzsblq) throws PwtzDatabaseException {
		if(_logger.isDebugEnabled()){
			_logger.debug("add:dyzsblq="+dyzsblq.toString());
		}
		if(dyzsblq==null){
			if(_logger.isDebugEnabled()){
				_logger.debug("add,dyzsblq is null");
			}
			return null;
		}else{
//			UUID uuid=UUID.randomUUID();
//			dyzsblq.setDeviceid(uuid.toString());
			dyzsblq.setStatus(1);
			dyzsblq.setCreatedate(new Date());
			dyzsblq.setLastmodifiedtime(new Date());
		}
		try{
			objMapper.insertSelective(dyzsblq);
		}catch(DataAccessException e){
			_logger.error(ExceptionUtils.getStackTrace(e));
			throw new PwtzDatabaseException();
		}
		return dyzsblq;
	}

	@Override
	public int delete(String deviceid) throws PwtzDatabaseException,CanNotDeleteException {
		if(_logger.isDebugEnabled()){
			_logger.debug("delete:deviceid="+deviceid);
		}
		int result=0;
		try{
			boolean bl=businessRefService.canDelete("T_DYZSBLQ", deviceid);
			if(!bl){
				throw new CanNotDeleteException();
			}
			result=objMapper.deleteById(deviceid,new Date());
		}catch(DataAccessException e){
			_logger.error(ExceptionUtils.getStackTrace(e));
			throw new PwtzDatabaseException();
		}
		return result;
	}

	@Override
	public int update(Dyzsblq dyzsblq) throws PwtzDatabaseException {
		if(_logger.isDebugEnabled()){
			_logger.debug("update:dyzsblq="+dyzsblq.toString());
		}
		int result=0;
		if(dyzsblq==null){
			if(_logger.isDebugEnabled()){
				_logger.debug("update,dyzsblq is null");
			}
			return result;
		}else{
		    dyzsblq.setLastmodifiedtime(new Date());
		}
		try{
			result=objMapper.updateByPrimaryKeySelective(dyzsblq);
		}catch(DataAccessException e){
			_logger.error(ExceptionUtils.getStackTrace(e));
			throw new PwtzDatabaseException();
		}
		return result;
	}

	@Override
	public PaginationList<Dyzsblq> selectDyzsblqs(String devicename,Long companyId,int portalType,String[] medLineCodes,String towerCode,String beginTime,
			String endTime, int pageSize, int pageNo)throws PwtzDatabaseException {
		PaginationList<Dyzsblq> paginationList=new PaginationList<Dyzsblq>();
		if(_logger.isInfoEnabled()){
		    _logger.info("findDybx:|devicename=" + devicename+"|beginTime=" + beginTime+"|endTime=" + endTime+"|companyId="+companyId+"|portalType="+portalType+"|medLineCodes="+PwtzUtils.parseCharacterArrayToString(medLineCodes)+"|towerCode="+towerCode+"|pageSize="+pageSize+"|pageNo="+pageNo);
		}
		List<Dyzsblq> list=null;
		int beginIndex=pageSize*pageNo-pageSize;
		String companyidStr="";
		String [] companyids=null;
		try{
			if(portalType==1){
				companyidStr=companyService.findChildrensId(companyId,null);
				//System.out.println(companyidStr);
				if(companyidStr!=null&&companyidStr.length()>0){
					companyidStr=companyidStr.substring(0, companyidStr.length()-1);
					companyids=companyidStr.split(",");
				}
				//System.out.println(companyids.length);
			}
			list=objMapper.selectDeviceList(devicename,portalType,medLineCodes,towerCode,companyids,companyId,beginTime, endTime, beginIndex, pageSize);
		}catch(DataAccessException e){
			_logger.error(ExceptionUtils.getStackTrace(e));
			throw new PwtzDatabaseException();
		}
		if(list!=null){
			_logger.debug("selectDyzsblqs:result list="+list.toString());
		}
		int totalCount = 0;
        int totalPage =1;
        if(pageSize!=0){
            try
            {
                totalCount = objMapper.selectDeviceCount(devicename,portalType,medLineCodes,towerCode,companyids,companyId,beginTime,endTime);
               // System.out.println(totalCount);
            }
            catch (DataAccessException e)
            {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }else{
            pageNo=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
		return paginationList;
	}
	
	@Override
    public PaginationList<Dyzsblq> selectDyzsblqByDytg(String towerCode,String deviceName,Long companyId,int pageSize, int pageNo) throws PwtzDatabaseException
    {
        PaginationList<Dyzsblq> paginationList = new PaginationList<Dyzsblq>();
        
        List<Dyzsblq> list = null;
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
            list = objMapper.selectByDytg(towerCode, deviceName, companyId, benginIndex, pageSize);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("selectDyzsblqByDytg:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage =1;
        if(pageSize!=0){
            try
            {
                totalCount = objMapper.selectDeviceCountByDytg(towerCode, deviceName, companyId);
            }
            catch (DataAccessException e)
            {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }else{
            pageNo=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }
}
